package design.Day02;

import java.util.ArrayList;
import java.util.List;

/**
* @description: 天气类
* @author: wangjun
* @create: 2022-01-11
*/
public class WeatherData implements Subject{

    private List<Observer> observerList = new ArrayList<>();

    private float temp;
    private float humidity;
    private float pressure;

    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        int index = observerList.indexOf(observer);
        if(index >= 0){
            observerList.remove(observer);
        }
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observerList) {
            System.out.println(observer);
            observer.update(temp, humidity, pressure);
        }
    }

    public void setField(float temp, float humidity, float pressure){
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        notifyObservers();
    }
}
